package com.hk.app.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.sys.ProductVerReq;
import com.hk.Mgr.web.message.sys.ProductVerRes;
import com.hk.Mgr.web.message.sys.SysRoleReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.ProductVer;
import com.hk.commons.entity.sys.SysUser;
import com.hk.service.IProductVer;
import com.hk.service.sys.ISysUser;

@Controller
@RequestMapping(value = "/pv")
public class ProductVerCtrl extends BaseCtrl {

	@Autowired
	private IProductVer productVerService;
	
	@Autowired
	private ISysUser sysUserService;

	@RequestMapping(value = "/chkver")
	@ResponseBody
	public ResponseBase<ProductVer> chkUpdate(@RequestParam String type) {
		ResponseBase<ProductVer> resp = new ResponseBase<>();
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("type", Short.valueOf(type));
		hqlFilter.addSort("cts");
		hqlFilter.addOrder("desc");

		List<ProductVer> list = productVerService.findByFilter(hqlFilter);

		resp.setResult(list.get(0));

		return resp;
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public ResponseBase<Boolean> save(@RequestBody ProductVer pv) {
		ResponseBase<Boolean> resp = new ResponseBase<>();

		Long id = null;
		if (pv != null) {
			id = productVerService.add(pv);
		}
		if (id != null)
			resp.setResult(true);
		else
			resp.setResult(false);

		return resp;
	}

	@RequestMapping(value = "/listPv")
	@ResponseBody
	public ResponseBase<List<ProductVerRes>> listPv(@RequestBody ProductVerReq req){
		 
		ResponseBase<List<ProductVerRes>> resp = new ResponseBase<>();
		List<ProductVerRes> resplist = new ArrayList<>(); 
		HqlFilter hqlFilter = new HqlFilter();
		try{
			
			if (StringUtils.isNotEmpty(req.getVerName())) {
				hqlFilter.addLKFilter("verName", req.getVerName());
			}
			
			if (StringUtils.isNotEmpty(req.getVer())) {
				hqlFilter.addLKFilter("ver", req.getVer());
			}
			
			hqlFilter.addSort("cts");
			hqlFilter.addOrder("desc");
	
			List<ProductVer> list = productVerService.findByFilter(hqlFilter, req.getCurPage(), req.getPageSize());
			for(ProductVer pv:list){
				ProductVerRes res = new ProductVerRes();
				ReqToModelHelper.copyReqValueToModel(pv, res);
				
				if(pv.getSponsor() != null){
					SysUser sponsor = sysUserService.findById(pv.getSponsor());
					if(sponsor != null && StringHelper.isNotEmpty(sponsor.getRealName())){
					res.setSponsorName(sponsor.getRealName());
					}
				}
				resplist.add(res);
			}
			resp.setResult(resplist);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setSize(list.size());
			
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		
		return resp;
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public ResponseBase<Boolean>  updatePv(@RequestBody ProductVer req){

		ResponseBase<Boolean> resp = new ResponseBase<>();
		
		if(req == null || req.getId() == null){
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			resp.setMsg("参数不对：id is null！");
			return resp;
		}else{
			productVerService.update(req);
			resp.setResult(true);
		}
		return resp;
	}
	

}
